global with sharing class RemoteActionForWeek {
    public RemoteActionForWeek(ApexPages.StandardController con){
        
    }
    @RemoteAction
    global static List<Map<String,String>> findUser(String userid,String CamName)
    {
        System.debug(LoggingLevel.INFO, '*** time1: ' );
       /* List<User> list_u=[select id,UserRole.ParentRoleId,UserRoleId from User where UserRole.ParentRoleId=:UserInfo.getUserRoleId()];
        Set<Id> set_subUs=new Set<Id>();
        Set<Id> set_userId=new Set<Id>();
        for(User ur:list_u)
        {
                set_userId.add(ur.Id);
                set_subUs.add(ur.UserRoleId);
        }
        // List<User> list_u2=[select id,UserRole.ParentRoleId,UserRoleId from User where UserRole.ParentRoleId in:set_subUs];
        // for(User ur2:list_u2)
        // {
        //      set_userId.add(ur2.Id);
                
        // }

        // 循环遍历子用户 modified by Harry 2017年03月07日

        // Map<Id,User> staticUserMap = new Map<Id,User>([SELECT id, UserRole.ParentRoleId,UserRoleId FROM User WHERE UserRole.ParentRoleId=:UserInfo.getUserRoleId()]);
        System.debug(LoggingLevel.INFO, '*** time2: ');

        // Set<Id> tmpUserRoleIdSet = new Set<Id>(set_subUs);
        // while(tmpUserRoleIdSet.size()>0){
        //     // List<User> tmpUserList = [select id,UserRole.ParentRoleId,UserRoleId from User where UserRole.ParentRoleId in:tmpUserRoleIdSet];
        //     List<User> tmpUserList = queryUser(tmpUserRoleIdSet,list_u);
        //     tmpUserRoleIdSet = new Set<Id>();
        //     for(User u : tmpUserList){
        //         tmpUserRoleIdSet.add(u.UserRoleId);
        //         set_userId.add(u.Id);
        //     }
        // }

        //
        Set<Id> roleIds = new Set<Id>();
        roleIds.add(UserInfo.getUserRoleId());
        for(User user: [select Id From User where UserRoleId in : getAllSubRoleIds(roleIds)]){
            set_userId.add(user.Id);
        }
            */
        System.debug(LoggingLevel.INFO, '*** time3: ');

        List<Map<String,String>> userList=new List<Map<String,String>>();
        String str='%'+CamName+'%';
        //List<User> uList=[select id,Name,UserRole.Name from User where id in:set_userId and Name like :str limit 10];
        List<User> uList=[select id,Name,UserRole.Name from User where  Name like :str limit 10];

        //system.debug(set_userId+'set_userIdset_userIdset_userId');
        for(User u:uList){
            
            Map<String,String> usermap=new Map<String,String>();
            usermap.put('id', u.Id+'');
            usermap.put('name', u.Name);
            usermap.put('onlyone',u.UserRole.Name);
            userList.add(usermap);
        }
        

        
        return userList;
    }
/*
    private static Set<ID> getAllSubRoleIds(Set<ID> roleIds) {
        Set<ID> currentRoleIds = new Set<ID>();
        for(UserRole userRole :[select Id from UserRole where ParentRoleId 
             IN :roleIds AND ParentRoleID != null]) {
            currentRoleIds.add(userRole.Id);
        }
        if(currentRoleIds.size() > 0) {
            currentRoleIds.addAll(getAllSubRoleIds(currentRoleIds));
        }
        return currentRoleIds;
    }
*/

    @RemoteAction
    global static List<Map<String,String>> findCam(String userid,String CamName)
    {
        List<Map<String,String>> userList=new List<Map<String,String>>();
        String str='%'+CamName+'%';
        
        List<Campaign> uList=[SELECT Id,Name FROM Campaign WHERE Name like :str limit 10];
        for(Campaign u:uList){
            
            Map<String,String> usermap=new Map<String,String>();
            usermap.put('id', u.Id+'');
            usermap.put('name', u.Name);
            usermap.put('onlyone',u.Name);
            userList.add(usermap);
        }
      
        return userList;
    }

    //业务机会
    @RemoteAction
    global static List<Map<String,String>> findOpp(String userid,String oppName){
    List<Map<String,String>> userList=new List<Map<String,String>>();
    
    String str='%'+oppName+'%';
    String user = System.UserInfo.getUserId();
    List<Opportunity> uList=[SELECT Id,Name,Account.Name,AccountId FROM Opportunity WHERE Name like :str and Is_Stop__c = false Limit 10];
    
    System.debug('*** uList: ' + uList + '');
    for(Opportunity u:uList){
       Map<String,String> usermap=new Map<String,String>();
       usermap.put('id', u.Id+'');
       usermap.put('name', u.Name);
       usermap.put('onlyone',u.Account.Name);
       usermap.put('accname',u.Account.Name);
       usermap.put('accid',u.AccountId+'');
       userList.add(usermap);
    }
     
       return userList;
    }
    //客户
    @RemoteAction
    global static List<Map<String,String>> findAcc(String accid,String accname){
     List<Map<String,String>> userList=new List<Map<String,String>>();
     String user = System.UserInfo.getUserId();
     String str='%'+accname+'%';
     List<Account> uList=[SELECT Id,Name,Address__c,NCAccountNumber__c FROM Account 
                         WHERE (Name like :str)   Limit 10];
     
     System.debug('*** uList: ' + uList + '');
     for(Account u:uList){
       Map<String,String> usermap=new Map<String,String>();
       usermap.put('id', u.Id+'');
       usermap.put('name', u.Name);
       usermap.put('onlyone', u.Address__c);
      String custcode = '';
       custcode = u.NCAccountNumber__c == null ? '':u.NCAccountNumber__c;
       usermap.put('custcode',custcode);
       userList.add(usermap);
     }
     
       return userList;
    }
    //联系人
    @RemoteAction
    global static List<Map<String,String>> findCon(String conid,String conname,String Accid){
        List<Map<String,String>> userList=new List<Map<String,String>>();
        String user = System.UserInfo.getUserId();
        String str='%'+conname+'%';
        List<Contact> uList=[SELECT Id,Name,MobilePhone,Account.Name,AccountId FROM Contact WHERE 
        (Name like :str or MobilePhone like :str or Email like :str)  and AccountId=:Accid Limit 10];
        
        System.debug('*** uList: ' + uList + '');
        for(Contact u:uList){
            Map<String,String> usermap=new Map<String,String>();
            usermap.put('id', u.Id+'');
            usermap.put('name', u.Name);
            usermap.put('onlyone',u.MobilePhone);
            usermap.put('accname',u.Account.Name);
            usermap.put('accid',u.AccountId+'');

            userList.add(usermap);
        }
     
       return userList;
    }
    
}